package com.lanbu.web.controller.camera;

import com.lanbu.camera.domain.CaWarn;
import com.lanbu.camera.service.ICaWarnService;
import com.lanbu.common.annotation.Log;
import com.lanbu.common.core.controller.BaseController;
import com.lanbu.common.core.domain.AjaxResult;
import com.lanbu.common.core.page.TableDataInfo;
import com.lanbu.common.enums.BusinessType;
import com.lanbu.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 摄像头预警Controller
 *
 * @author lanbu
 * @date 2024-03-12
 */
@Api(tags = "摄像头预警")
@RestController
@RequestMapping("/camera/warn")
public class CaWarnController extends BaseController {
    @Autowired
    private ICaWarnService caWarnService;

    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 查询摄像头预警列表分页
     */
    @ApiOperation(value = "查询摄像头预警列表分页")
    @PreAuthorize("@ss.hasPermi('camera:warn:query')")
    @GetMapping("/list")
    public TableDataInfo listTable(CaWarn caWarn) {
        startPage();
        List<CaWarn> list = caWarnService.selectCaWarnList(caWarn);
        return getDataTable(list);
    }

    /**
     * 查询摄像头预警列表不分页
     */
    @ApiOperation(value = "查询摄像头预警列表不分页")
    @PreAuthorize("@ss.hasPermi('camera:warn:query')")
    @GetMapping("/list/no")
    public AjaxResult list(CaWarn caWarn) {
        return success(caWarnService.selectCaWarnList(caWarn));
    }


    /**
     * 获取摄像头预警详细信息
     */
    @ApiOperation(value = "通过id获取摄像头预警详细信息")
    @PreAuthorize("@ss.hasPermi('camera:warn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(caWarnService.selectCaWarnById(id));
    }


    /**
     * 通过摄像头Id查询摄像头预警
     */
    @ApiOperation(value = "通过摄像头Id查询摄像头预警")
    @PreAuthorize("@ss.hasPermi('camera:warn:query')")
    @GetMapping(value = "/warnByCameraId/{id}")
    public AjaxResult warnByCameraId(@PathVariable("id") String id) {
        return success(caWarnService.warnByCameraId(id));
    }

    /**
     * 获取未处理的预警信息数量
     */
    @ApiOperation(value = "获取未处理的预警信息数量")
    @PreAuthorize("@ss.hasPermi('camera:warn:query')")
    @GetMapping(value = "/warnCount")
    public AjaxResult warnCount() {
        return success(caWarnService.warnCount());
    }


    /**
     * 导出摄像头预警列表
     */
    @ApiOperation(value = "导出摄像头预警列表")
    @PreAuthorize("@ss.hasPermi('camera:warn:export')")
    @Log(title = "摄像头预警", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CaWarn caWarn) {
        List<CaWarn> list = caWarnService.selectCaWarnList(caWarn);
        ExcelUtil<CaWarn> util = new ExcelUtil<CaWarn>(CaWarn.class);
        util.exportExcel(response, list, "摄像头预警数据");
    }

    /**
     * 新增摄像头预警
     */
    @ApiOperation(value = "新增摄像头预警")
    @PreAuthorize("@ss.hasPermi('camera:warn:add')")
    @Log(title = "摄像头预警", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CaWarn caWarn) {
        return toAjax(caWarnService.insertCaWarn(caWarn));
    }


    /**
     * 删除摄像头预警
     */
    @ApiOperation(value = "删除摄像头预警")
    @PreAuthorize("@ss.hasPermi('camera:warn:remove')")
    @Log(title = "摄像头预警", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(caWarnService.deleteCaWarnByIds(ids));
    }


    /**
     * 通过Id对预警信息查看
     */
    @ApiOperation(value = "通过Id对预警信息查看")
    @PreAuthorize("@ss.hasPermi('camera:warn:query')")
    @GetMapping(value = "/warnInformationByCameraId/{id}")
    public AjaxResult warnInformationByCameraId(@PathVariable("id") Long ids) {
        return success(caWarnService.warnInformationByCameraId(ids));
    }

//    /**
//     * 通过Id对预警信息处理
//     */
//    @ApiOperation(value = "通过Id对预警信息处理")
//    @PreAuthorize("@ss.hasPermi('camera:warn:edit')")
//    @GetMapping(value = "/warnHandleByCameraId/{id}")
//    public AjaxResult warnHandleByCameraId(@PathVariable("id") Long[] ids) {
//        caWarnService.warnHandleByCameraId(ids);
//        webSocketServer.sendToAllClient(caWarnService.warnCount().toString());
//        return success(1);
//    }

    /**
     * 修改摄像头预警
     */
    @ApiOperation(value = "修改摄像头预警")
    @PreAuthorize("@ss.hasPermi('camera:warn:edit')")
    @Log(title = "摄像头预警", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CaWarn caWarn) {
        if (caWarn.getWarnHandle() != null && caWarn.getWarnHandle().equals("1")) {
            webSocketServer.sendToAllClient(caWarnService.warnCount().toString());
        }
        return toAjax(caWarnService.updateCaWarn(caWarn));
    }
}
